package cn.service.Impl;

import cn.dao.TemplateDao;
import cn.domain.Template;
import cn.service.TemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2017/4/24.
 */
@Service
@Transactional
public class TemplateServiceImpl implements TemplateService {
    @Resource
    private TemplateDao templateDao;

    public void add() {
    }

    @Override
    public List<Template> findAll() {
        return templateDao.findAll();
    }

    @Override
    public void save(Template model) {
        templateDao.save(model);
    }

    @Override
    public void delete(Long id) {
        templateDao.delete(id);
    }

    @Override
    public Template getById(Long id) {
        return templateDao.getById(id);
    }

    @Override
    public void update(Template template) {
        templateDao.update(template);
    }

    /**
     * 根据模板id获取此模板对应的文件输入流
     * @param id
     * @return
     */
    @Override
    public InputStream getInputStreamById(Long id) {
        Template template=templateDao.getById(id);
        String path=template.getFilePath();
        InputStream in=null;
        try {
           in=new FileInputStream(new File(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return in;
    }
}
